package com.dnb.webmash.playmaker.client.mediaplayer;

import java.util.ArrayList;

import com.dnb.webmash.playmaker.client.playertypes.EmbedPlayer;
import com.dnb.webmash.playmaker.client.playertypes.FilePlayer;
import com.dnb.webmash.playmaker.client.playertypes.FlashPlayer;
import com.dnb.webmash.playmaker.client.playertypes.IFramePlayer;
import com.dnb.webmash.playmaker.client.playertypes.ScriptPlayer;
import com.dnb.webmash.playmaker.shared.PlayerConfig;
import com.dnb.webmash.playmaker.shared.PlaylistItem;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DeckPanel;
import com.dnb.webmash.playmaker.shared.Option;

public class MediaPlayersManager extends Composite{
	
	private PlaylistItem mediaIt;	
	private DeckPanel players;
	private int selected = -1;
	private PlayerConfig playerprefs;
	private boolean settingsEnabled;
	
	public MediaPlayersManager() {
		this.settingsEnabled = false;
		this.players = new DeckPanel();
		players.add(new FlashPlayer());//TODO use playerdefs to make multiple mediaplayers ie. new mediaplayer(embedplayer)
		players.add(new IFramePlayer());
		players.add(new FilePlayer());
		players.add(new EmbedPlayer());
		players.add(new ScriptPlayer());
		this.initWidget(players);
	}//TWO MORE POSSIBLE CONSTRUCTORS: 1 WITH MEDIAITEM AS PARAM, 1 WITH PLAYERPREFS AS PARAM...
	
	public boolean embedItem(PlaylistItem mediaItem) {//TODO deal with stopping currently playing video before switching
		if (mediaItem!=null){
			this.mediaIt = mediaItem;
			for (int i = 0; i < players.getWidgetCount(); i++){
				MediaPlayer mp = (MediaPlayer) players.getWidget(i);
				if (this.mediaIt.getPlayerType().equals(mp.getType())){
					this.selected = i;
					players.showWidget(i);
					if ((this.settingsEnabled)&&(this.playerprefs!=null)) mp.applySettings(this.playerprefs);
					mp.embedItem(mediaItem);										
					return true;
				}
			}			
		}
		return false;
	}	
	@Override
	public void setSize(String width, String height){
		super.setSize(width, height);
		this.setWidth(width);
		this.setHeight(height);
	}
	@Override
	public void setWidth(String width) {//TODO add to prefs
		super.setWidth(width);
		for (int i = 0; i < players.getWidgetCount(); i++){
			MediaPlayer mp = (MediaPlayer) players.getWidget(i);
			mp.setWidth(width);
		}
	}
	@Override
	public void setHeight(String height) {
		super.setHeight(height);
		for (int i = 0; i < players.getWidgetCount(); i++){
			MediaPlayer mp = (MediaPlayer) players.getWidget(i);
			mp.setHeight(height);
		}
	}			
	public void addStateChangedHandler(MediaPlayerStateChangedHandler medHandler) {
		// TODO implement
		// set isPlaying
	}
	public MediaPlayer getCurrentPlayer(){
		if (selected != -1)
			return (MediaPlayer) players.getWidget(selected);
		else return null;
	}	

	public void initSettings(PlayerConfig settings) {
		this.playerprefs = settings;
	}

	//Whether to apply the stored settings to the current player before embedding
	public void enableSettings(Boolean value) {		
		this.settingsEnabled = value;		
	}
	public ArrayList<Option> getCurrentOptions(){
		if (this.playerprefs==null)
			return null;
		else
			return playerprefs.getOptions();		
	}

	public boolean hasPrefs() {
		if (this.playerprefs !=null)
			return true;
		return false;
	}
}
